<?php
namespace Anls\Controller;
use Think\Controller;
use Anls\Com\File;
use Anls\Com\Bar;
use Anls\Com\Strategy;
use Anls\Common;
use Anls\Common\Log;
/**
 * 模拟交易
 * @author wsf
 *
 */
class TradeController extends AbstractTradeSupportController{
    
    
    public function log(){
    	
        $this->display();
    }
    
    public function getLog(){
        $log = Common\Redis::getLog($this->redis);
        echo $log;
    }
    
    public function index(){
        $this->display();
    }
    
    public function getTradeLog(){
        $code = I('code');
        $key = Common\Redis::$TRADE_LOG_KEY.$code;
        $logs = $this->redis->lRange($key, 0, -1);
        echo json_encode($logs);
    }
    
    public function getAnlsData(){
        $codes = $this->redis->zRange(Common\Redis::$TRADE_CODE_KEY, 0, -1);
        $codeTradeLog = array();
        $len = count($codes);
        $dataList = array();
        for($i=0;$i<$len;$i++){
            $code = $codes[$i];
            $key = Common\Redis::$TRADE_ANLS_KEY . $code;
            $val = $this->redis->get($key);
            $dataList[$code] = $val;
        }
        echo json_encode($dataList);
    }
    
	/**
	 * 模拟交易入口
	 */
	function exec(){
		// 品种
		$symbolType = I('symbolType');
		// 周期
		$period = I('period');
		// 指定代码
		$code = I('code'); 
		// 开仓策略表达式
	    $openStrategyExpr = I('openStrategyExpr');
		// 平仓策略表达式
	    $closeStrategyExpr = I('closeStrategyExpr');
		 // 交易类型 buy / sell
	    $tradeType = I('tradeType');
		Log::info('开始模拟交易.....');
		
		// 获取分析文件
		$fileList = File\FileHandle::getFiles($symbolType,$period);
		$fileListCnt = count($fileList);
		Log::info('获取分析文件:'.$fileListCnt);
		// 获取过滤代码
		$filterCodes = $this->getFilterCodes();
		Log::info('获取过滤代码:'.count($filterCodes));
		
		$BarH = new Bar\BarHandle();
		Common\Redis::clearLog($this->redis);
		$this->redis->set(Common\Redis::$CONSOLE_ALL_KEY,$fileListCnt-1);
		$rst = array();
		$rstFilter = array();
		$begin = 0;
		$end = $fileListCnt;
		
		//$begin = 996;
		//$end = $begin + 500;
		for($fi=$begin;$fi<$end;$fi++){
		    // ------------ 统计时间 begin ---------------
		    $t_total_begin = microtime(true);
		    // ------------ 统计时间 begin ---------------
		    
		    //$this->redis->set(Common\Redis::$CONSOLE_CUR_KEY,$fi);
			// 获取文件名
			$fileName = $fileList[$fi];
			if($code != null){
			    if($code != $fileName){
			        continue;
			    }
			} else {
			    if($filterCodes[$fileName] == 1) {
			    	//array_push($rst,$filterCodes[$fileName]);
			    	//continue;
			    }
			}
			
			//Common\Redis::log($this->redis, '开始分析：'.$fileName);
			// 获取文件内容
			$content = File\FileHandle::getFileContentByFileName($symbolType,$period,$fileName);
			if(!strlen($content)>0)continue;
			
			// 获取K线信息
			$bar = $BarH->getBarInfo($content);
			// 策略测试
			$StrategyMain = new Strategy\Main($bar, $symbolType, $fileName, $period,$tradeType,$openStrategyExpr,$closeStrategyExpr);
			$StrategyMain->trade();
			unset($StrategyMain);
			//array_push($rst,$fileName);
			if($code != null && $code == $fileName){
			    break;
			}
			
			// ------------ 统计时间 end ---------------
			$t_total_end = microtime(true);
			Common\Log::echoPage($fi.'__'.$fileName.':用时：'.round($t_total_end-$t_total_begin,5).'秒</br>');
			// ------------ 统计时间 end ---------------
			
		}
		Log::info('执行完成!');
	}
	
	/**
	 * 重设缓存
	 */
	private function resetCache(){
	    $this->redis->del(Common\Redis::$TRADE_CODE_KEY);
	    Common\Redis::deleteKeys($this->redis, Common\Redis::$TRADE_LOG_KEY);
	    Common\Redis::deleteKeys($this->redis, Common\Redis::$TRADE_ANLS_KEY);
	}
	

}